package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.OrderTotalBean;
import com.wuliupai.delivery.wuliupaidelivery.constants.InterfaceNet;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.DateUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 订单统计界面
 */
@EActivity(R.layout.activity_order_statistics)
public class OrderStatisticsActivity extends AppBaseActivity {

    @Bean(DateUtils.class)
    DateUtils timeUtilsCustom;

    @ViewById(R.id.tv_repertoryStatisticsStartAndEndTime)
    TextView tv_repertoryStatisticsStartAndEndTime;

    @ViewById(R.id.tv_inTotal)
    TextView tv_inTotal;
    @ViewById(R.id.tv_inComplete)
    TextView tv_inComplete;
    @ViewById(R.id.tv_inCancel)
    TextView tv_inCancel;

    @ViewById(R.id.tv_outTotal)
    TextView tv_outTotal;
    @ViewById(R.id.tv_outComplete)
    TextView tv_outComplete;
    @ViewById(R.id.tv_outCancel)
    TextView tv_outCancel;

    private String startTime;

    @AfterViews
    void onPageStart() {
        setDefaultTitle(this, R.string.string_orderStatistics_title);

        startTime = timeUtilsCustom.formatMillsecond2String(System.currentTimeMillis());
        String[] date = timeUtilsCustom.getFirstDayAndLastDayOfLastOrNextMonth(startTime, 0, true);
        getRepertoryTotal(date[0], date[1]);
        tv_repertoryStatisticsStartAndEndTime.setText(
                String.format(getString(R.string.string_orderStatistics_startAndEndTime), date[0], date[1]));
    }

    /**
     * 获取仓库统计
     */
    private void getRepertoryTotal(String startTime, String endTime) {
        OrderTotalBean requestBean = new OrderTotalBean(startTime, endTime);
        String requestJson = JsonUtils.createJsonString(requestBean);
        try {
            String requestAes = JsonUtils.aes(requestJson);
            callNet(this, InterfaceNet.Net_GetOrderTotal, requestAes,
                    new NetResultOperate() {
                        @Override
                        public void onSuccess(String content) {
                            Object obj = JsonUtils.parseJsonString(content, OrderTotalBean.class);
                            if (obj instanceof OrderTotalBean) {
                                OrderTotalBean bean = (OrderTotalBean) obj;
                                tv_inTotal.setText(bean.getConsigneeTotalCount());
                                tv_inComplete.setText(bean.getConsigneeCompleteCount());
                                tv_inCancel.setText(bean.getConsigneeCancelCount());

                                tv_outTotal.setText(bean.getConsignorTotalCount());
                                tv_outComplete.setText(bean.getConsignorCompleteCount());
                                tv_outCancel.setText(bean.getConsignorCancelCount());
                            }
                        }

                        @Override
                        public void onDataEmpty(String content) {
                        }

                        @Override
                        public void onDataEnd(String content) {
                        }

                    });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 上个月
     */
    @Click(R.id.btn_orderStatisticsForwardBtn)
    void onRepertoryStatisticsForwardClick() {
        getTime(-1);
    }

    /**
     * 下个月
     */
    @Click(R.id.btn_orderStatisticsNextBtn)
    void onRepertoryStatisticsNextClick() {
        getTime(1);
    }

    /**
     * 获取日期
     *
     * @param monthInterval 月份间隔
     */
    private void getTime(int monthInterval) {
        String[] date = timeUtilsCustom.getFirstDayAndLastDayOfLastOrNextMonth(startTime, monthInterval, true);
        startTime = date[0];
        getRepertoryTotal(date[0], date[1]);
        tv_repertoryStatisticsStartAndEndTime.setText(
                String.format(getString(R.string.string_orderStatistics_startAndEndTime), date[0], date[1]));
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        animUtils.leftOutAnimation(this);
    }
}
